/**
 * Copyright (C), 2020-2021 by colley.ma
 * File Name: SwakAutoConfiguration.java
 * Encoding: UTF-8
 * Date: 2021年3月31日 上午11:25:08
 * History:
*/
package com.swak.frame.spring.configuration;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.swak.frame.SwakConstants;
import com.swak.frame.cache.CacheProxy;
import com.swak.frame.config.ServerRegisterProperties;
import com.swak.frame.server.ServerHeatbeartMonitor;
import com.swak.frame.server.ServerRegisterConfig;
import com.swak.frame.server.ServerRegisterService;
import com.swak.frame.server.ServerRegisterServiceImpl;

@Configuration
@EnableConfigurationProperties(ServerRegisterProperties.class)
@ConditionalOnProperty(prefix = SwakConstants.SWAK_SERVER, name = "monitor", havingValue = "true", matchIfMissing = false)
public class ServerRegisterConfiguration {

	@Bean
	@ConditionalOnMissingBean(ServerRegisterService.class)
	public ServerRegisterService serverRegisterService(final ServerRegisterProperties serverRegisterProperties,
			final CacheProxy cacheProxy) {
		ServerRegisterServiceImpl serverRegisterService = new ServerRegisterServiceImpl();
		serverRegisterService.setServerconfig(new ServerRegisterConfig(serverRegisterProperties.getHeatPeriod(),
				serverRegisterProperties.getHeatDelay()));
		serverRegisterService.setCacheProxy(cacheProxy);
		return serverRegisterService;
	}

	@Bean
	@ConditionalOnMissingBean(ServerHeatbeartMonitor.class)
	public ServerHeatbeartMonitor serverHeatbeartMonitor(final ServerRegisterProperties serverRegisterProperties,
			final ServerRegisterService serverRegisterService, final CacheProxy cacheProxy) {
		ServerHeatbeartMonitor serverHeatbeartMonitor = new ServerHeatbeartMonitor();
		serverHeatbeartMonitor.setEnabled(serverRegisterProperties.isMonitor());
		serverHeatbeartMonitor.setServerRegisterService(serverRegisterService);
		return serverHeatbeartMonitor;
	}
}
